###############################
# Pre-analysis
###############################
# Load packages
library(readstata13)
library(tidyverse)

# Set your working directory
setwd("~/Dropbox/2001_replication/draft/data")

# Load data from replication files
ess <- read.dta13("input/rueda_additaly.dta")

# Select variables 
dat <- ess %>% dplyr::select(essround, cntry, year, idno, 
                             redpref1, redpref2, redpref3,
                             brncntr, incomedif, forpop,
                             age, gender, education, religion, union, 
                             euroesec, socgdp, lrscale, aesfdrk)

# Recode redistribution preferences, religion, and union as numeric variables
dat <- dat %>% mutate(redpref1 = recode(redpref1,
                                        "agree or strongly agree" = 1,
                                        "neither agree nor disagree, disagree, strongly disagree" = 0),
                      redpref2 = recode(redpref2,
                                        "strongly agree" = 1,
                                        "agree, neither agree nor disagree, disagree, strongly disagree" = 0),
                      redpref3 = recode(redpref3,
                                        "strongly agree" = 5,
                                        "agree" = 4,
                                        "neither agree nor disagree" = 3,
                                        "disagree" = 2,
                                        "strongly disagree" = 1),
                      brncntr = recode(brncntr, "Yes" = 1, "No" = 0),
                      religion = recode(religion,
                                        "every day, more than once a week, once a week" = 1,
                                        "less than once a week" = 0),
                      union = recode(union, "yes, currently" = 1,
                                     "no, or yes previously" = 0)
)


dat <- dat %>% mutate(euroesec = as.factor(euroesec),
                      year = as.factor(year),
                      redpref3 = as.factor(redpref3))

# Type of welfare regimes
dat$welfare <- ""
dat$welfare[dat$cntry %in% c("BE", "DE", "NL", "AT", "CH", "FR")] <- "corporatist"
dat$welfare[dat$cntry %in% c("DK", "SE", "NO", "FI")] <- "social_dem"
dat$welfare[dat$cntry %in% c("IE", "GB")] <- "liberal"
dat$welfare[dat$cntry %in% c("IT", "ES", "PT", "GR")] <- "southern"

########################################
# To do further analysis, 
# we will use other variables in ESS that are not included in Rueda's dataset.
# We need to merge some variables from cumulative ESS data.
########################################

# Cumulative ESS data that include ALL variables in ESS 1-6
ess_cum <- read.csv("input/ESS1-8e01.csv", stringsAsFactors = FALSE)

# Select variables from the cumulative data
ess_merge <- ess_cum %>% dplyr::select(cntry, essround, idno, 
                                       # include variables you want to use
                                       gvcldcr, # Child care services for working parents, governments' responsibility
                                       gvslvue, # Standard of living for the unemployed, governments' responsibility 
                                       imdfetn, # Allow many/few immigrants of different race/ethnic group from majority
                                       impcntr # Allow many/few immigrants from poorer countries outside Europe
                                       ) 

# Recode
ess_merge <- ess_merge %>% mutate(childcare = ifelse(gvcldcr %in% c(77, 88, 99), NA, gvcldcr),
                                  unemployed = ifelse(gvslvue %in% c(77, 88, 99), NA, gvslvue),
                                  imm_etn = recode(imdfetn, `1` = 4, `2` = 3, `3` = 2, `4` = 1), # higher value: Agree
                                  imm_pcntr =  recode(impcntr, `1` = 4, `2` = 3, `3` = 2, `4` = 1)) # higher value: Agree

# Merge with our data
dat <- dat %>% left_join(ess_merge, by = c("cntry", "essround", "idno")) 

# Add Migrant Care Worker Ratio
mcwr <- read.csv("output/mcwr.csv", stringsAsFactors = FALSE)
mcwr$year <- as.character(mcwr$year)
dat <- dat %>% left_join(mcwr, by = c("cntry","year"))

# Create a dummy variable for MCWR
dat$d_mcwr <- ifelse(dat$mcwr > mean(mcwr$mcwr,na.rm = T), 1, 0)

write.csv(dat, "output/rueda_rep.csv", row.names = FALSE)
